#-*- coding:utf-8 -*-

from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.comments.models import Comment
from django.contrib.contenttypes.models import ContentType 
from business.models import Shop, SType, Activity
from business.forms import CreateShopForm, AddProductForm, AddActivityForm

SHOP_NUM_OF_PAGE = 20


def shop_list(request, type_id, page_num):
    _typeid = int(type_id)
    _pagenum = int(page_num)

    if _typeid == 0:
        shop_qs = Shop.objects.all()
    else:
        shop_qs = Shop.objects.filter(stype_id = _typeid)

    paginator = Paginator(shop_qs, SHOP_NUM_OF_PAGE)
    try:
        shop_list = paginator.page(page_num)
    except EmptyPage:
        shop_list = paginator.page(paginator.num_pages)

    return render(request, "business/shop_list.html", {'shop_list': shop_list, 'page':paginator})


def shop_detail(request, shop_id):
    _shopid = int(shop_id)
    shop = get_object_or_404(Shop, pk=_shopid)
    return render(request, "business/shop_detail.html", {"shop":shop})


def product_list(request, shop_id, page_num):
    _shopid = int(shop_id)
    _pagenum = int(page_num)
    shop = get_object_or_404(Shop, pk=_shopid)
    product_qs = shop.product_set.all()

    paginator = Paginator(product_qs, )
    try:
        product_list = paginator.page(page_num)
    except EmptyPage:
        product_list = paginator.page(paginator.num_pages)

    return render(request, "business/product_list.html", {"product_list":product_list})

def product_detail(request, product_id):
    _productid = int(product_id)
    product = get_object_or_404(Product, pk=_productid)

    return render(request, "business/product_detail.html", {"product":product})


def activity_list(request, shop_id):
    _shopid = int(shop_id)
    _pagenum = int(page_num)
    shop = get_object_or_404(Shop, pk=_shopid)
    act_qs = shop.product_set.all()

    paginator = Paginator(product_qs, SHOP_NUM_OF_PAGE)
    try:
        act_list = paginator.page(page_num)
    except EmptyPage:
        act_list = paginator.page(paginator.num_pages)

    return render(request, "business/activity_list.html", {"activity_list":activitiy_list, "paginator":paginator})


def activity_detail(request, activity_id):
    _actid = int(activity_id)
    activity = get_object_or_404(Activity, pk=_actid)
    return render(request, "business/activity_detail.html", {"activity":activity})
   

def message_list(request, shop_id, page_num):
    _shopid = int(shop_id)
    _pagenum = int(page_num)

    shop = get_object_or_404(Shop, pk=_shopid)
    obj_type = ContentType.objects.get_for_model(shop)
    msg_qs = Comment.objects.filter(content_type_id=obj_type.id, object_id=shop.id)
    paginator = Paginator(msg_qs, SHOP_NUM_OF_PAGE)
    try:
        msg_list = paginator.page(page_num)
    except EmptyPage:
        msg_list = paginator.page(paginator.num_pages)

    return render(request, "business/activity_list.html", {"msg_list":msg_list, "paginator":paginator})

    
@login_required
def create_shop(request):
    if request.method == "GET":
        form = CreateShopForm()
        return render(request, "business/create_shop.html", {"form":form})

    form = CreateShopForm(request.POST)
    if form.is_valid():
        shop = form.save(False)
        shop.owner = request.user
        shop.save()
        return render(request, "business/create_shop_ok.html")

    else:
        return render(request, "business/create_shop.html", {"form":form})


@login_required
def add_product(request, shop_id):
    _shopid = int(shop_id)
    shop = get_object_or_404(Shop, pk=_shopid)

    if request.method == "GET":
        form = AddProductForm()
        return render(request, "business/add_product.html", {"form":form})

    form = AddProductForm(request.POST)
    if form.is_valid():
        product = form.save(False)
        product.shop = shop 
        product.save()
        return render(request, "business/add_product_ok.html")

    else:
        return render(request, "business/add_product.html", {"form":form})


@login_required
def add_activity(request, shop_id):
    _shopid = int(shop_id)
    shop = get_object_or_404(Shop, pk=_shopid)

    if request.method == "GET":
        form = AddActivityForm()
        return render(request, "business/add_activity.html", {"form":form})

    form = AddActivityForm(request.POST)
    if form.is_valid():
        activity = form.save(False)
        activity.shop = shop 
        activity.save()
        return render(request, "business/add_activity_ok.html")

    else:
        return render(request, "business/add_activity.html", {"form":form})

